package com.mtw.bbs.article.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mtw.bbs.article.pojo.entity.ArticleFile;
import com.mtw.bbs.article.mapper.ArticleFileMapper;
import com.mtw.bbs.article.service.ArticleFileService;
import com.mtw.bbs.common.core.vo.BaseFileVo;
import com.mtw.bbs.common.core.vo.Result;
import com.mtw.bbs.fileAdmin.feign.FileClient;
import com.mtw.bbs.fileAdmin.pojo.vo.FileVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 附件逻辑层
 */
@Slf4j
@RequiredArgsConstructor
@Service("articleFileService")
public class ArticleFileServiceImpl extends ServiceImpl<ArticleFileMapper, ArticleFile> implements ArticleFileService {

    private final FileClient fileClient;



    /**
     * 根据文章id集合获取关联的附件
     * @param articleIds                文章id集合
     * @return                          附件vo集合
     */
    @Override
    public Map<String,List<BaseFileVo>> getFileMap(List<String> articleIds) {

        if (CollectionUtil.isEmpty(articleIds)){
            return new HashMap<>(1);
        }
        LambdaQueryWrapper<ArticleFile> lqw = new LambdaQueryWrapper<>();
        lqw.in(ArticleFile::getArticleId,articleIds);
        List<ArticleFile> list = list(lqw);
        if (CollectionUtil.isEmpty(list)){
            return new HashMap<>(1);
        }

        Set<String> set = list.stream().map(ArticleFile::getFileId).collect(Collectors.toSet());
        Result<List<FileVo>> result1 = fileClient.getFileUrlByIdArray(set.toArray(String[]::new));
        if (!result1.isSuccess() || CollectionUtil.isEmpty(result1.getData())){
            return new HashMap<>(1);
        }

        Map<String, FileVo> fileVoMap = result1.getData().stream().collect(Collectors.toMap(FileVo::getId, Function.identity(), (x1, x2) -> x2));
        Map<String, List<BaseFileVo>> fileMap = new HashMap<>(list.size());
        list.forEach(f->{
            List<BaseFileVo> files = fileMap.computeIfAbsent(f.getArticleId(), k -> new ArrayList<>(9));
            FileVo vo1 = fileVoMap.get(f.getFileId());
            if (vo1 != null){
                files.add(vo1);
            }
        });
        return fileMap;
    }


    /**
     * 根据文章id获取关联的附件
     * @param articleId                 文章id
     * @return                          附件vo集合
     */
    @Cacheable(value = "articleFileList", key = "#articleId")
    @Override
    public List<BaseFileVo> getFileList(String articleId) {

        LambdaQueryWrapper<ArticleFile> lqw = new LambdaQueryWrapper<>();
        lqw.select(ArticleFile::getFileId);
        lqw.eq(ArticleFile::getArticleId,articleId);
        List<ArticleFile> list = list(lqw);
        if (list.isEmpty()){
            return new ArrayList<>(1);
        }

        Set<String> set = list.stream().map(ArticleFile::getFileId).collect(Collectors.toSet());
        Result<List<FileVo>> fileResult = fileClient.getFileUrlByIdArray(set.toArray(String[]::new));
        if (!fileResult.isSuccess() || CollectionUtil.isEmpty(fileResult.getData())){
            return new ArrayList<>(1);
        }
        Map<String, FileVo> fileVoMap = fileResult.getData().stream().collect(Collectors.toMap(FileVo::getId, Function.identity(), (x1, x2) -> x2));

        List<BaseFileVo> result = new ArrayList<>();
        for (ArticleFile x : list) {
            FileVo vo = fileVoMap.get(x.getFileId());
            if (vo != null) {
                result.add(vo);
            }
        }
        return result;
    }
}
